// scrollView锚点
export function scrollView(id) {
  const eleDOM = document.getElementById(id);
  if (eleDOM) {
    eleDOM.scrollIntoView({
      behavior: 'smooth', // auto
      block: 'start'
    });
  }
}

// 文本脱敏
export const textDesensitization = (str, isPhone) => {
  if (str) {
    str = str.toString();
    if (str.length === 1) {
      return str;
    } else if (str.length === 2) {
      return str.charAt(0) + '*';
    } else if (isPhone || str.length === 11) {
      return str.slice(0, 3) + '****' + str.slice(7);
    } else {
      return str.charAt(0) + '*' + str.charAt(str.length - 1);
    }
  } else {
    return str;
  }
};

// 密码校验
export const pwdRulesText = '密码需由8-16位字母、数字、特殊字符(~@$%^&*.#_)任意组合'; // 密码正则中文提示
export const isPwd = pwd => {
  const reg = /^(?![A-z0-9]+$)(?![A-z~@$#%^&*._]+$)(?![0-9~@#$%^&*._]+$)([A-z0-9~@$%#^&*._]{8,16})$/;
  return reg.test(pwd);
};

// 手机号校验
export const isPhone = phone => /^1[3456789]\d{9}$/.test(phone);

// 身份证校验
export const isIdCard = idcard => {
  const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  return reg.test(idcard);
};

// 小数点输入框
export const decimalInput = (v = '', digit) => {
  if (digit === 2) {
    return v.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1'); // 2
  } else {
    return v;
  }
};

// 数字输入框,有maxNumber说明有最大限制,isDecimal为true说明可以为小数（2位）
export const integerInput = (v = '', maxNumber, isDecimal) => {
  v = v.toString();
  const result = isDecimal ? v.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1') : v.replace(/[^\d]/g, '');
  if (maxNumber) {
    if (Number(result) > Number(maxNumber)) {
      return maxNumber;
    } else {
      if (v) {
        return Number(result) ? result : 1;
      } else {
        return '';
      }
    }
  } else {
    return result;
  }
};

// 通过身份证获取性别及生日
export const getBirthdayAndSexByIdCard = (idCard) => {
  if (idCard) {
    const obj = {
      birthday: "",
      sex: ''
    };
    if (idCard.length === 15) {
      obj.birthday = "19" + idCard.slice(6, 12);
    } else if (idCard.length === 18) {
      obj.birthday = idCard.slice(6, 14);
    }
    obj.birthday = obj.birthday.replace(/(.{4})(.{2})/, "$1-$2-");
    obj.sex = parseInt(idCard.slice(-2, -1)) % 2; // 1男 0女
    return obj;
  }
};

// 获取数组尾值
export const getArrLast = value => {
  return Array.isArray(value) ? value[value.length - 1] : value;
};

// 将[[],[]]里的末值拼用,接起来
export const getArrLastJoin = (doubleArr) => {
  if (Array.isArray(doubleArr)) {
    const childArr = [];
    doubleArr.forEach(i => childArr.push(getArrLast(i)));
    return childArr.join(',');
  } else {
    return doubleArr;
  }
};

// 处理查询栏参数
export const formatQuery = (obj) => {
  const query = {};
  Object.keys(obj).forEach(key => {
    if (Array.isArray(obj[key])) {
      if (Array.isArray(obj[key][0])) {
        query[key] = getArrLastJoin(obj[key]);
      } else {
        query[key] = getArrLast(obj[key]);
      }
    } else {
      query[key] = obj[key];
    }
  });
  return query;
};

// 通过生日获取年龄
export const getBirthDayByIdCard = idCard => {
  let birthday = '';
  if (idCard) {
    birthday = idCard.substr(6, 8);
    birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
  }
  return birthday;
};

// 获取当日  yyyy-MM-dd
export const getToday = () => {
  const date = new Date();
  const year = date.getFullYear();
  const m = (date.getMonth() + 1 < 10 ? '0' : '') + (date.getMonth() + 1);
  const d = (date.getDate() < 10 ? '0' : '') + date.getDate();
  return `${year}-${m}-${d}`;
};

// 获取时分秒 HH:mm:ss
export const getHMS = () => {
  const date = new Date();
  const h = date.getHours();
  const m = date.getMinutes();
  const s = date.getSeconds();
  const h1 = h < 10 ? '0' + h : h;
  const m1 = m < 10 ? '0' + m : m;
  const s1 = s < 10 ? '0' + s : s;
  return `${h1}：${m1}：${s1}`;
};

// 获取天时分秒 d:h:m:s
export function secondsFormat(time) {
  if (time) {
    const day = Math.floor(time / (24 * 3600));
    const h = Math.floor((time - day * 24 * 3600) / 3600);
    const hour = h > 9 ? h : '0' + h;
    const m = Math.floor((time - day * 24 * 3600 - hour * 3600) / 60);
    const minute = m > 9 ? m : '0' + m;
    const s = time - day * 24 * 3600 - hour * 3600 - minute * 60;
    const second = s > 9 ? s : '0' + s;
    let str = '';
    if (day) str = str + day + '天';
    if (hour) str = str + hour + ':';
    if (minute) str = str + minute + ':';
    if (second) str = str + second;
    return str;
  }
}

// filterPerRouter
export const filterPerRouter = (allMenuList, userRight) => {
  return allMenuList
    .filter(item => {
      if (item.meta) {
        return userRight.includes(item.meta.menuId) || !item.meta.menuId;
      } else {
        return item;
      }
    })
    .map(item => {
      item = Object.assign({}, item);
      if (item.children) item.children = filterPerRouter(item.children, userRight);
      return item;
    });
};

// downloadFile - url
export function downloadFileByUrl(fileName, path) {
  const link = document.createElement('a');
  link.style.display = 'none';
  link.href = path;
  link.setAttribute('download', fileName);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

// downloadFile - blob
export function downloadFile(obj, name, suffix) {
  const url = window.URL.createObjectURL(
    new Blob([obj], {
      type: 'application/vnd.ms-excel'
    })
  );
  const link = document.createElement('a');
  link.style.display = 'none';
  link.href = url;
  const fileName = name + suffix;
  link.setAttribute('download', fileName);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

// 对比前后端数据类型自适应转换 integer string
// export const comparisonValType = (web, java) => {
//   if (web === java) {
//     return web;
//   } else {
//     if ((web || web === 0) && java) {
//       const webType = typeof web;
//       const javaType = typeof java;
//       const isNumOrStr = (webType === 'string' || webType === 'number') && (javaType === 'string' || javaType === 'number');
//       if (isNumOrStr) {
//         return javaType === 'string' ? web.toString() : Number(web);
//       } else {
//         return web;
//       }
//     } else {
//       return java === 0 ? 0 : web;
//     }
//   }
// };
